﻿using IRepositoryBll;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositoryEntity.Extension;
using RepositorySystem.Filter;
using RepositorySystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static RepositoryEntity.WorkFlow_InstanceStep;

namespace RepositorySystem.Controllers
{
    /// <summary>
    /// 工作流程表
    /// </summary>
    public class WorkFlow_InstanceStepController : Controller
    {
        private IWorkFlow_InstanceStepBll _workFlow_InstanceStepBll;
        public WorkFlow_InstanceStepController(IWorkFlow_InstanceStepBll workFlow_InstanceStepBll)
        {
            _workFlow_InstanceStepBll = workFlow_InstanceStepBll;
        }

        /// <summary>
        /// 工作流程表页面
        /// </summary>
        /// <returns></returns>
        [CustomActionFilter]
        public IActionResult WorkFlow_InstanceStepListView()
        {
            return View();
        }

        /// <summary>
        /// 获取工作流程页面（分页）
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="reviewStatus"></param>
        /// <returns></returns>
        public IActionResult GetWorkFlow_InstanceStepList(int page, int limit, InstanceStepStatus reviewStatus)
        {
            int count = 0;
            //获取登录用户
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                return Json(new
                {
                    code = 0,
                    msg = "成功",
                    count = count,
                    data = new List<object>()
                });
            }

            object WorkFlow_InstanceSteps = _workFlow_InstanceStepBll.GetWorkFlow_InstanceStepListByPage(page, limit, out count, reviewStatus, userInfo.ID);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = WorkFlow_InstanceSteps
            });
        }

        /// <summary>
        /// 修改工作流程页面
        /// </summary>
        /// <returns></returns>
        public IActionResult UpWorkFlow_InstanceStepView()
        {
            
            return View();
        }

        /// <summary>
        /// 更新修改修改工作流程
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="ReviewerReason"></param>
        /// <param name="ReviewerStatus"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateWorkFlow_InstanceStep(string ID ,string ReviewerReason , InstanceStepStatus ReviewerStatus)
        {
            CustomActionResult res = new CustomActionResult();
            //审核状态： 1:通过，2:审核通过，3:驳回，其他数字提交上来无效
            if (ReviewerStatus == InstanceStepStatus.同意 || ReviewerStatus == InstanceStepStatus.驳回)
            {
                UserInfo userInfo = this.GetSessionUserInfo();
                if (userInfo == null)
                {
                    res.Msg = "当前用户非合法";
                    return Json(res);
                }
                //审批业务操作
                bool isSuccess = _workFlow_InstanceStepBll.Review(ID, ReviewerReason, ReviewerStatus,out string msg, userInfo);
                if (isSuccess)
                {
                    res.IsSuccess = true;
                    res.Msg = "成功";
                    res.Status = 1;
                    return Json(res);
                }
                else
                {
                    return Json(res);
                }
            }
            else
            {
                res.Msg = "参数有误";
                return Json(res);
            }
        }

        /// <summary>
        /// 通过流程实例步骤id查询他的流程实例
        /// </summary>
        /// <param name="workFlow_InstanceStepId"></param>
        /// <returns></returns>
        public IActionResult GetWorkFlow_Instance(string workFlow_InstanceStepId)
        {
            CustomActionResult res = new CustomActionResult();
            var instance = _workFlow_InstanceStepBll.GetWorkFlow_InstanceByStepId(workFlow_InstanceStepId);
            if (instance != null)
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                res.Status = 1;
                res.Data = instance;
            }

            return Json(res);
        }
    }
}
